//
//
//  AiShangJinFu2.0
//
//  Created by AiShangJinFu2.0 on 2018/10/5.
//  Copyright © 2018年 AiShangJinFu2.0. All rights reserved.
//


#import "MyActionWithInformation.h"
#import "Mediator+Login.h"
#import "Mediator+My.h"

NSString * const  NSNotificationPushToSystemMessageController = @"NSNotificationPushToSystemMessageController";

@interface MyActionWithInformation ()
@property(nonatomic,strong)UIWindow * rootWindow;
@end
@implementation MyActionWithInformation
-(instancetype)initConfig {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(haveMessageToPush) name:NSNotificationPushToSystemMessageController object:nil];
    }
    return self;
}
+(MyActionWithInformation *)mananger {
    static MyActionWithInformation * instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[MyActionWithInformation alloc] initConfig];
    });
    return instance;
}
- (void)haveMessageToPush {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [[HFQCommonTools lastWindow].rootViewController presentViewController:[[Mediator sharedInstance] My_MessageViewController] animated:YES completion:nil];
        });
}
-(BOOL)getMessageStatusWithApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSDictionary *userInfo= launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        NSLog(@"点击远程通知启动的程序");
        NSString *strURL = userInfo[@"SkipURL"];
        if (strURL) {
            NSURL *url = [NSURL URLWithString:strURL];
            [[UIApplication sharedApplication] openURL:url];
        }
        [self setMessageNoread];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:NSNotificationPushToSystemMessageController object:nil];
        });
    }else{
        if ([UIApplication sharedApplication].applicationIconBadgeNumber>0) {
            NSLog(@"直接点击App图标启动的程序 且有消息");
            [self setMessageNoread];
        }else{
            NSLog(@"直接点击App图标启动的程序 且无消息");
        }
    }
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    return YES;
}
-(void)getMessgaeWhthApplicationDidBecomeActive:(UIApplication *)application {
    long messgaeNum = [UIApplication sharedApplication].applicationIconBadgeNumber;
    NSLog(@"%ld",messgaeNum);
    if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0) {
        [self setMessageNoread];
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
        [JPUSHService resetBadge];
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
    } else {
    }
    [self getUnreadMessageNum];
}
-(void)getMessageWhenApplicationWillEnterForeground:(UIApplication *)application {
    long messgaeNum = [UIApplication sharedApplication].applicationIconBadgeNumber;
    NSLog(@"%ld",messgaeNum);
    if ([UIApplication sharedApplication].applicationIconBadgeNumber>0) {
        [self setMessageNoread];
    }
}
-(void)getMessageNumWhenApplication:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo window:(UIWindow *)window {
    self.rootWindow = window;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NSNotificationHaveNewNewMessage" object:nil];
    [self setMessageNoread];
    if (application.applicationState == UIApplicationStateActive) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"有新消息，是否查看" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *actionSure = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self haveMessageToPush];
            }];
            UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertC addAction:actionSure];
            [alertC addAction:actionCancel];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertC animated:YES completion:nil];
        });
    } else if(application.applicationState == UIApplicationStateInactive){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:NSNotificationPushToSystemMessageController object:nil];
        });
    } else {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:NSNotificationPushToSystemMessageController object:nil];
        });
    }
}
-(void)setMessageNoread {
    
    [AD_UserDefaults setObject:@"1" forKey:@"setMessageNoread"];
    [AD_UserDefaults synchronize];
}
-(void)getUnreadMessageNum {
}



/*
 代码不可删，保证无bug
 */
//---------------------------------------------------------------------------------------------------------------------
+(NSString *)libraryPath{
    NSArray *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    return [[libraryPath objectAtIndex:0] stringByAppendingPathComponent:@"Caches"];
}
+(NSString *)tmpPath{
    NSString *tmpPathStr = NSTemporaryDirectory();
    return tmpPathStr;
}

+(NSString *)documentsPath {
    NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [documentsPath objectAtIndex:0];
}
+ (BOOL)writeDictionary:(NSMutableDictionary *)dic toFilePath:(NSString *)filePath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    return  [fileManager createFileAtPath:filePath contents:data attributes:nil];
}
+ (BOOL)writeArray:(NSMutableArray *)arr toFilePath:(NSString *)filePath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
    NSData *data = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil];
    return  [fileManager createFileAtPath:filePath contents:data attributes:nil];
}
+ (BOOL)writeString:(NSString *)str toFilePath:(NSString *)filePath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    return  [fileManager createFileAtPath:filePath contents:data attributes:nil];
}
+ (NSMutableDictionary *)readDictionaryWithFilePath:(NSString *)filePath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData *data = [fileManager contentsAtPath:filePath];
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        return dic;
    } else {
        return nil;
    }
}
+ (NSMutableArray *)readArrayWithFilePath:(NSString *)filePath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData *data = [fileManager contentsAtPath:filePath];
        NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        return arr;
    } else {
        return nil;
    }
}
+ (NSString *)readStringWithFilePath:(NSString *)filePath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        return [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    } else {
        return nil;
    }
}
//------------------------------------------------------------------------------------------------------------------------------

@end
